import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { UserInfoDto } from './dto/userinfo.dto';
import { UserInfo } from './entities/userinfo.entity';

@Injectable()
export class UserInfoService {
  constructor(
    @InjectModel('UserInfos') private readonly userInfoModel: Model<UserInfo>,
  ) {}
  async create(userInfoDto: UserInfoDto) {
    return await this.userInfoModel.insertMany(userInfoDto);
  }

  async findAll(): Promise<UserInfoDto[]> {
    return await this.userInfoModel.find().exec();
  }
  
  async findOne(account: string, id: number): Promise<UserInfoDto[]> {
    if (account) return await this.userInfoModel.find({ account });
    if (id) return await this.userInfoModel.find({ id });
  }

  async update(id: number, userInfoDto: UserInfoDto) {
    return await this.userInfoModel.updateOne(
      { id },
      {
        $set: {
          AppId: userInfoDto.AppId,
        },
      },
    );
  }

  async remove(id: string): Promise<any> {
    return await this.userInfoModel.deleteOne({ _id: id });
  }
}
